package com.unisrv.portlet;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class HelloPortlet extends GenericPortlet {
	protected String editJSP;
	protected String viewJSP;
	
	@Override
	public void init() throws PortletException{
		editJSP = getInitParameter("edit-jsp");
		viewJSP = getInitParameter("view-jsp");
	}
	
	@Override
	public void doEdit(RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		
		renderResponse.setContentType("text/html");
		PortletURL addNameUrl = renderResponse.createActionURL();
		addNameUrl.setParameter("addName", "add");
		//System.out.println(addNameUrl.toString());
		renderRequest.setAttribute("addNameUrl", addNameUrl.toString());
		include(editJSP, renderRequest, renderResponse);
	}
	
	@Override
	public void doView(RenderRequest renderRequest, 
			RenderResponse renderResponse) throws IOException, PortletException {
		
		PortletPreferences prefs = renderRequest.getPreferences();
		String username = prefs.getValue("name", "no");
		System.out.println("name:"+username);
		if(username.equalsIgnoreCase("no")) {
			username = "default name";
		}
		renderRequest.setAttribute("username", username);
	}

//	@Override
//	public void processAction(ActionRequest actionRequest,
//			ActionResponse actionResponse) throws IOException, PortletException {
//		
//		String addName = actionRequest.getParameter("addName");
////		System.out.println("test:"+actionRequest.getParameter("test"));
//		System.out.println("addName:"+addName);
//		if(addName != null){
//			PortletPreferences prefs = actionRequest.getPreferences();
//			prefs.setValue("name", actionRequest.getParameter("username"));
//			System.out.println("actionRequest:"+actionRequest.getParameter("username"));
//			prefs.store();
//			actionResponse.setPortletMode(PortletMode.VIEW);
//		}
//	}
	
	public void editUser(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		
		String addName = actionRequest.getParameter("addName");
//		System.out.println("test:"+actionRequest.getParameter("test"));
		System.out.println("addName:"+addName);
		if(addName != null){
			PortletPreferences prefs = actionRequest.getPreferences();
			prefs.setValue("name", actionRequest.getParameter("username"));
			System.out.println("actionRequest:"+actionRequest.getParameter("username"));
			prefs.store();
			actionResponse.setPortletMode(PortletMode.VIEW);
		}
	}
	
	protected void include(String path, RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		
		PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher(path);
		
		if(portletRequestDispatcher==null){
			System.out.println(path+" is not valid");
		} else {
			portletRequestDispatcher.include(renderRequest, renderResponse);
		}
	}
}
